package edu.nf.bs.controller;

import com.github.pagehelper.PageInfo;
import edu.nf.bs.dao.UserInfoMapper;
import edu.nf.bs.dao.impl.UserInfoMapperImpl;
import edu.nf.bs.entity.UserInfo;
import edu.nf.bs.exception.LoginException;
import edu.nf.bs.service.UserInfoService;
import edu.nf.bs.service.impl.UserInfoServiceImpl;
import edu.nf.bs.util.HttpUtil;
import edu.nf.bs.util.UUIDUtil;
import edu.nf.bs.vo.ResultVO;
import org.mvc.web.annotation.RequestMapping;
import org.mvc.web.servlet.View;
import org.mvc.web.view.JsonView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class UserInfoController extends BaseContraller{
    @RequestMapping("/user/login")
    public View login(UserInfo userInfo, HttpSession session){
        UserInfoMapper mapper=new UserInfoMapperImpl();
        //创建用户业务逻辑类
        UserInfoServiceImpl service = new UserInfoServiceImpl();
        service.setUserInfoMapper(mapper);
        try {
            UserInfo user = service.checkUser(userInfo);
            //将用户保存到session
            session.setAttribute("user", user);
            UserInfo userInfo1 =(UserInfo) session.getAttribute("user");
            //返回成功的ResultVO

            ResultVO successVo = success(user);

            return new JsonView(successVo);
        } catch (LoginException e) {
            e.printStackTrace();
            //响应错误内容的vo
            ResultVO failVo = fail(401, e.getMessage());
            return new JsonView(failVo);
        }
    }
    @RequestMapping("/userInfo/list")
    public View listUser(String userPhone, Integer page, Integer limit) {
        UserInfoMapper mapper=new UserInfoMapperImpl();
        UserInfoServiceImpl service = new UserInfoServiceImpl();
        service.setUserInfoMapper(mapper);
        PageInfo<UserInfo> pageInfo = service.listUser(userPhone, page, limit);
        ResultVO<List<UserInfo>> vo = page(pageInfo);
        JsonView jsonView = new JsonView(vo);
        return jsonView;
    }
    @RequestMapping("/userInfo/addUer")
    public View addUser(UserInfo userInfo){
        userInfo.setUserId(Long.toString(UUIDUtil.randomNum()));
        userInfo.setUserCard(null);
        userInfo.setUserPig("default.jpg");
        userInfo.setUserStatus(1);
        userInfo.setUserVip(Long.toString(UUIDUtil.randomNum()));
        UserInfoMapper mapper=new UserInfoMapperImpl();
        UserInfoServiceImpl service = new UserInfoServiceImpl();
        service.setUserInfoMapper(mapper);
        service.addUser(userInfo);
        ResultVO success = success();
        return new JsonView(success);
    }

    @RequestMapping("/userInfo/uploadPicture")
    public View uploadPicture(String userId,HttpServletRequest request) throws ServletException, IOException {
        //获取layui的字段名
        Part part = request.getPart("file");
        String userPig = part.getSubmittedFileName();
        UserInfoMapper mapper = new UserInfoMapperImpl();
        //System.out.println(userId);
        UserInfoServiceImpl service = new UserInfoServiceImpl() ;
        service.setUserInfoMapper(mapper);
        service.updateHeaderPictureById(userPig,userId,part) ;
        return new JsonView("{\"msg\":\"ok\"}") ;
    }
    @RequestMapping("/user/updateUser")
    public View updateUser(UserInfo userInfo){
        System.out.println(userInfo);
        UserInfoMapper mapper=new UserInfoMapperImpl();
        UserInfoServiceImpl userInfoService = new UserInfoServiceImpl();
        userInfoService.setUserInfoMapper(mapper);
        userInfoService.updateUser(userInfo);
        ResultVO success = success();
        return new JsonView(success);
    }

    @RequestMapping("/user/updateUserPhone")
    public View updatePhone(String userPhone,String userId){
        UserInfoMapper mapper=new UserInfoMapperImpl();
        UserInfoServiceImpl userInfoService = new UserInfoServiceImpl();
        userInfoService.setUserInfoMapper(mapper);
        userInfoService.updateUserPhone(userPhone,userId) ;
        ResultVO success = success();
        return new JsonView(success);
    }

    @RequestMapping("/user/updateUserPassword")
    public View updatePassword(String userPassword,String userId){
        UserInfoMapper mapper=new UserInfoMapperImpl();
        UserInfoServiceImpl userInfoService = new UserInfoServiceImpl();
        userInfoService.setUserInfoMapper(mapper);
        userInfoService.updateUserPassword(userPassword,userId) ;
        ResultVO success = success();
        return new JsonView(success);
    }

    @RequestMapping("/user/queryUser")
    public View querUser(String userId){
        UserInfoService service = new UserInfoServiceImpl() ;
        UserInfo userInfo = service.queryUser(userId);
        ResultVO<UserInfo> vo = success(userInfo) ;
        return new JsonView(vo) ;
    }

}
